let ApigwClient = require('./indexPost');

class KingdeeAuxInfo {

    /**
     * 构造器
     */
    constructor(clientID, clientSecret, appToken) {
        this.clientID = clientID;
        this.clientSecret = clientSecret;
        this.appToken = appToken;
    }


    /**
     * 获取辅助资料列表
     */
    async getAuxInfoList(params) {
        let list = [];
        try {
            let requestSDK = new ApigwClient(this.clientID, this.clientSecret, this.appToken);
            const api = 'https://api.kingdee.com/jdy/v2/bd/aux_info';
            const resp = await requestSDK.request({
                url: api,
                method: 'get',
                params,
                data: {
                    page_size: 100
                }
            });
            let total = resp.data.data.total_page;
            for (let i = 1; i < total+1; i++) {
                params.page = i;
                const resp = await requestSDK.request({
                    // 调用api获取商品列表
                    url: api,
                    method: 'get',
                    params
                });
                for (let row of resp.data.data.rows) {
                    list.push(row);
                }
            }
        } catch (e) {
            console.error('获取辅助资料列表错误', e);
            throw e;
        }
        return list;
    }

    /**
     * 获取辅助资料分类列表
     */
    async getAuxInfoTypeList(params) {
        let list = [];
        try {
            let requestSDK = new ApigwClient(this.clientID, this.clientSecret, this.appToken);
            const api = 'https://api.kingdee.com/jdy/v2/bd/aux_info_type';
            const resp = await requestSDK.request({
                url: api,
                method: 'get',
                params,
                data: {
                    page_size: 100
                }
            });
            if(await resp.data.data.count==="0"){
                console.error('没有此基础资料分类');
                return "没有此数据";
            }
            let total = resp.data.data.total_page;
            for (let i = 1; i < total+1; i++) {
                params.page = i;
                const resp = await requestSDK.request({
                    // 调用api获取商品列表
                    url: api,
                    method: 'get',
                    params
                });
                for (let row of resp.data.data.rows) {
                    list.push(row);
                }
            }
            return list;
        } catch (e) {
            console.error('获取辅助资料分类列表错误', e);
            throw e;
        }

    }
}

module.exports = KingdeeAuxInfo;
